<?php
class Core_Model_DbTable_Transactions extends Zend_Db_Table_Abstract {
    protected $_name = 'transactions';
    const prefix_ref = "EPAC_";
    
    public function generateRefNo() {
        $last_id = $this->_insertTrackingRow();
        if ($last_id < 10000) {
            $float_suffix = strval((float)($last_id / 10000));
            $suffix_ref = substr($float_suffix, 2);
        } else {
            $suffix_ref = $last_id;
        }
        return self::prefix_ref . $suffix_ref;
    }
    
    private function _insertTrackingRow() {
        $session = Zend_Registry::get("session");
        $user_id = $session->user->id;
        
        $tracking_data = array (
            'input_user' => $user_id,
            'created_time' => time()
        );
        
        $tracking_db = new Core_Model_DbTable_TrackingInput();
        return $tracking_db->insert($tracking_data);
    }
        
    public function updateDunnageOperationId($ref_no, $dunnage_operation_id) {
        $this->update(array("dunnage_operation_id" => $dunnage_operation_id), "ref_no = '$ref_no'");
    }
    
    public function updateCargoOperationId($ref_no, $cargo_operation_id) {
        $this->update(array("cargo_operation_id" => $cargo_operation_id), "ref_no = '$ref_no'");
    }
    
    public function updateTallyOperationId($ref_no, $tally_operation_id) {
        $this->update(array("tally_operation_id" => $tally_operation_id), "ref_no = '$ref_no'");
    }
    
    public function getTrackingShips($filters, $offset = 0, $limit = SHIP_PER_PAGE) {
        $select = $this->select()
                       ->from($this->_name)
                       ->limit($limit, $offset);
        $transactions = $this->fetchAll($select)->toArray();
        $general_info_db = new Core_Model_DbTable_GeneralInformation();
        $port_db = new Core_Model_DbTable_Ports();
        foreach ($transactions as &$transaction) {
            $general_info = $general_info_db->get($transaction['general_information_id']);
            $transaction['general_info'] = $general_info;
            $transaction['port'] = $port_db->get($general_info['port_id']);
        }
        return $transactions;
    }

    public function getTransaction($id = -1){
        $role = Core_Util::getRole();
        $session = Zend_Registry::get("session");
        $user_id = isset($session->user)?$session->user->id:"";
        switch($role){
            case Core_Role::ROLE_PRINCIPAL:

                break;
            case Core_Role::ROLE_INPUT:
                break;
            case Core_Role::ROLE_ADMIN:
                break;
            case Core_Role::ROLE_GUEST:
            default:
                break;
        }
        if($id == -1){
            return null;
        }
        $cond = $this->getAdapter()->quoteInto("id = (?)", $id);
        return $this->fetchRow($cond);
    }
    
    public function getPaginatorAdapter($filters) {

        $select = $this->select()
                       ->setIntegrityCheck(false)
                       ->from($this->_name . ' AS trans')
                       ->join("general_information AS gi", 'trans.general_information_id = gi.id');
        if ( !empty($filters['ref_no']) ) {
            $cond = $this->getAdapter()->quoteInto("ref_no = ?", $filters['ref_no']);
            $select->where($cond);
        }
        if ( !empty($filters['ship_name']) ) {
            $cond = $this->getAdapter()->quoteInto("ship_name LIKE %?%", $filters['ship_name']);
            $select->where($cond);
        }
        if ( !empty($filters['port_of_call']) ) {
            $cond = $this->getAdapter()->quoteInto("port_id = ?", $filters['port_of_call']);
            $select->where($cond);
        }
        if ( !empty($filters['ata']) ) {
            $date = date("Y-m-d", $filters['ata']);
            $cond = $this->getAdapter()->quoteInto("FROM_UNIXTIME(ata, '%Y-%m-%d') = ?", $date);
            $select->where($cond);
        }
        if ( !empty($filters['ats']) ) {
            $date = date("Y-m-d", $filters['ats']);
            $cond = $this->getAdapter()->quoteInto("FROM_UNIXTIME(ats, '%Y-%m-%d') = ?", $date);
            $select->where($cond);
        }

        $role = Core_Util::getRole();
        $session = Zend_Registry::get("session");
        $id = isset($session->user) ? $session->user->id : null;

        switch (strtolower($role)) {
            case Core_Role::ROLE_PRINCIPAL:
                $select->setIntegrityCheck(false);
                $select->where("gi.id is not null");
                $select->where("gi.customer_id = {$id}");
                break;
            case Core_Role::ROLE_INPUT:
                $select->where($this->getAdapter()->quoteInto("input_user = (?)", $id));
                break;
            case Core_Role::ROLE_ADMIN:
                break;
            case Core_Role::ROLE_GUEST:

            default:
                return null;
        }
        $select->order("trans.created_time DESC");
        return new Zend_Paginator_Adapter_DbSelect($select);
    }
}
?>
